package com.android.music.prolyric;

import java.util.ArrayList;
import java.util.HashMap;

import com.android.music.prolyric.database.DatabaseManger;
import com.android.music.prolyric.search.ConnectionDetector;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class SearchActivity extends Activity
{
	private EditText edSearch;
	private ImageButton btnSearch;
	private TextView tvResult;
	private ListView lvResult;
	private ProgressBar processBar;
	private IntentFilter intentFilter;
	private ConnectionDetector cd;
	private SharedPreferences prefs;
	int colorSong,colorSinger,colorId,imgBgr,colorResult;
	int defaultColor = 0xffffffff;
	private RelativeLayout layoutSearch;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_activity);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        
        prefs = getSharedPreferences("searchColor", MODE_PRIVATE);
        cd = new ConnectionDetector(getBaseContext());
        edSearch = (EditText) findViewById(R.id.etSearch);
        btnSearch = (ImageButton) findViewById(R.id.btnSearchActi);
        tvResult = (TextView) findViewById(R.id.tvSearchCountActi);
        lvResult = (ListView) findViewById(R.id.lvResultSearch);
        layoutSearch = (RelativeLayout)findViewById(R.id.layoutSearch);
        processBar = (ProgressBar) findViewById(R.id.progressBarSearchActi);
        processBar.setVisibility(8);
        
              
        intentFilter = new IntentFilter();
        intentFilter.addAction("lOAD_SONG_FROM_WEB");
        registerReceiver(receiver, intentFilter);
        
        setColor();
       
        if(DatabaseManger.simpleAdapter!=null)
        	lvResult.setAdapter(DatabaseManger.simpleAdapter);
        btnSearch.setOnClickListener(new OnClickListener() 
        {
			
			public void onClick(View v)
			{
				lvResult.setEnabled(false);
				tvResult.setText("Searching...");
				processBar.setVisibility(0);
				
				if(cd.isConnectingToInternet())
				{
					String s = edSearch.getText().toString();
					Intent i = new Intent(getBaseContext(),LoadListSongOnline.class);								
					i.putExtra("songname",s);
					startService(i);
				}
				else
					{
						processBar.setVisibility(8);
						tvResult.setText("Can't connect to network...");
					}
				
			}
		});
        
        lvResult.setOnItemClickListener(new OnItemClickListener() 
        {

			public void onItemClick(AdapterView<?> arg0, View v, int pos,long id) 
			{
				if(cd.isConnectingToInternet())	
				{
					HashMap<String, String> hm = DatabaseManger.arrayList.get(pos);
					String[] data = {
							hm.get("title"),
							hm.get("singer"),
							hm.get("source"),
							hm.get("url")
					};
					
					for(int i=0;i<data.length;i++)
						Log.i(""+ i, data[i]);
					if(data!=null)
					{
						
						Intent broadcastIntent = new Intent();
						broadcastIntent.setAction("DATA_PLAY");
						broadcastIntent.putExtra("dataonline", data);
						getBaseContext().sendBroadcast(broadcastIntent);
						unregisterReceiver(receiver);
						processBar.setVisibility(View.VISIBLE);
						finish();
					}
				}
				else
					tvResult.setText("Can't connect to network...");					
				
			}
		});
        
	}
	
	private BroadcastReceiver receiver = new BroadcastReceiver()
	{

		@Override
		public void onReceive(Context context, Intent intent) 
		{
			Bundle extras = intent.getExtras();
			if(extras!=null)
			{
				lvResult.setEnabled(true);
				
				DatabaseManger.arrayList = (ArrayList<HashMap<String, String>>) extras.getSerializable("songlist");
				if(DatabaseManger.arrayList!=null)
				{
					processBar.setVisibility(8);
					
					DatabaseManger.simpleAdapter = new SimpleAdapterSearch(SearchActivity.this, DatabaseManger.arrayList, R.layout.search_row_acti,new String[] {"id","title","singer"},new int[]{R.id.tvSearchID,R.id.tvSearchSong,R.id.tvSearchSinger},colorId,colorSinger,colorSong);
					
					lvResult.setAdapter(DatabaseManger.simpleAdapter);  
					
					tvResult.setText( DatabaseManger.arrayList.size()+" songs found");	
				}
				
				else
				{
					processBar.setVisibility(8);
					tvResult.setText("No result");
				}
			}
		}
		
	};
	
	private void setColor()
	{
		colorSong= (int) prefs.getInt("songcolor",defaultColor);
		colorSinger= (int) prefs.getInt("singercolor",defaultColor);
		colorId=(int) prefs.getInt("idcolor",defaultColor);
		colorResult=(int) prefs.getInt("resultcolor",defaultColor);
		imgBgr = (int) prefs.getInt("sbgr",R.drawable.girl);
		layoutSearch.setBackgroundDrawable(getResources().getDrawable(imgBgr));
		tvResult.setTextColor(colorResult);
	}	
	
}
